<template>
  <van-list
      v-model:loading="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
  >
    <div v-for="item in list" :key="item.sn">
      <van-card @click="click(item.sn)">
        <template #title>
          <div style="font-size: large; font-weight: bold;">{{ item.displayName }}</div>
          <br>
        </template>
        <template #desc>
          SN:{{ item.sn }}<br>
        </template>
        <template #thumb>
          <icon-robot size="75"/>
        </template>
        <template #tags>
          version:{{ item.version }}
        </template>
      </van-card>
    </div>

  </van-list>


</template>
<script>
import {IconRobot} from "@arco-design/web-vue/es/icon";
import axios from "axios";
import {showToast} from "vant";
import 'vant/es/toast/style';
import switch_page from "@/js/switch_page";
import {cmdChannel} from "@/js/on_start";
import {Message} from "@arco-design/web-vue";
import handleExtAttr from "@/js/dev";
import {checkStates} from "@/js/axios_util";

export default {
  name: 'dev_list',
  components: {
    IconRobot
  }, data() {
    return {
      list: [],
      devTab: {},
      loading: false,
      finished: false
    }
  },
  setup() {
    const onLoad = () => {
    }
    return {onLoad}
  }, methods: {
    listChildren(node, holdData) {
      let url = '/dm/list';
      if (process.env.NODE_ENV === 'development') {
        url = window.dev.httpUrlBase + url;
      }
      axios.get(url, {
        node: node
      }).then((res) => {
        holdData(res.data);
      }).catch(checkStates)
    },
    click(sn) {
      let devMo = this.devTab[sn];
      window.currentLightDev = devMo
      showToast(devMo.displayName);
      let devType = devMo.devType;
      let routeTarget = switch_page.m[devType];
      if (!routeTarget) {
        Message.info({content: `${devType} is not support`, showIcon: true});
        return;
      }
      switch_page.switchPageTo(routeTarget, {sn: sn});
    }
  }, created() {
    this.listChildren('0', (data) => {
      for (let i = 0; i < data.length; i++) {
        let item = handleExtAttr(data[i]);
        this.devTab[item.sn] = item
        this.list.push(item);
      }

    })
    this.loading = false;
    this.finished = true;
    // 打开当前页的时候是登录成功了
    if (!cmdChannel.isConnected()) {
      cmdChannel.connect((eve) => {
        if ('ok' !== eve.data) {
          Message.error({id:`${eve.data}`,content: `${eve.data}`, showIcon: true, duration: 2000})
        }
      })
    }

    this.wesock = cmdChannel
  },
}
</script>